<template>
  <div>
    <el-row type="flex" :gutter="20" style="padding-left: 20px">
      <h2 style="margin-top: 23px; margin-left: 5px">安全专栏</h2>
      <el-form :inline="true"  class="demo-form-inline" style="margin-left: 50px; margin-top: 20px">
        <el-form-item label="时间范围：">
          <div class="block">
            <el-date-picker
              v-model="range"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item>
          <el-input placeholder="搜索通知标题、通知内容" prefix-icon="el-icon-search" v-model="searchInfo"> </el-input>
        </el-form-item>
        <el-button class="el-blue-button" @click="getSecurityNotices"><i class="el-icon-search"></i> &nbsp;查询</el-button>
        <el-button class="el-blue-button" @click="goview('新增安全通知')"><i class="el-icon-plus"></i>&nbsp;新增</el-button>
        <el-button class="el-blue-button"><i class="el-icon-refresh-right"></i>&nbsp;重置</el-button>
      </el-form>
    </el-row>
    <el-table
      :data="securityNotices"
      style="width: 100%"
      max-height="768">
      <el-table-column type="index" label="序号" width="150" align="center"></el-table-column>
      <el-table-column prop="title" label="标题" width="400" align="center"></el-table-column>
      <el-table-column prop="content" label="发布内容" align="center" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="date" label="发布时间" width="250" align="center"></el-table-column>
      <el-table-column label="操作" width="230" align="center">
        <template slot-scope="scope">
          <el-button class="el-blue-button" size="mini" @click="goViewWithQuery('查看安全通知', scope.row.id)">查看</el-button>
          <el-button class="el-blue-button" size="mini" @click="goViewWithQuery('编辑安全通知', scope.row.id)">修改</el-button>
          <el-button class="el-blue-button" size="mini" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
// import axios from 'axios'
import request from '@/network/request'

export default {
  name: 'index',
  data () {
    return {
      range: ['', ''],
      searchInfo: '',
      securityNotices: []
    }
  },
  methods: {
    goview (name) {
      this.$router.push({ name }).catch(err => {
        err && console.log('刷新') // 待优化
      })
    },
    goViewWithQuery (name, data) {
      console.log('请求' + data + '通知')
      this.$router.push({ name, query: { data: data } }).catch(err => {
        err && console.log('刷新') // 待优化
      })
    },
    getSecurityNotices: function () {
      console.log('开始日期' + this.range[0] + '截止日期' + this.range[1])
      request({
        method: 'get',
        url: '/bulletin/showsafetycolumns',
        params: {
          endtime: this.range[1],
          starttime: this.range[0],
          keyword: this.searchInfo
        }
      }).then(res => {
        console.log(res.data)// 请求的返回体
        this.securityNotices = res.data.data
      }).catch((error) => {
        console.log(error)// 异常
      })
    },
    handleDelete (index, row) {
      request({
        method: 'get',
        params: {
          id: row.id
        }
      }).then((res) => {
        console.log(res.data)// 请求的返回体
        this.getSecurityNotices()
      }).catch((error) => {
        console.log(error)// 异常
      })
    }
  },
  created () {
    const nowDate = new Date()
    const nowdate = {
      year: nowDate.getFullYear(),
      month: nowDate.getMonth() + 1,
      date: nowDate.getDate()
    }
    console.log(nowDate)
    const futureMonthDate = new Date()
    futureMonthDate.setTime(nowDate.getTime() + 1000 * 60 * 60 * 24 * 30)
    const futureMonthdate = {
      year: futureMonthDate.getFullYear(),
      month: futureMonthDate.getMonth() + 1,
      date: futureMonthDate.getDate()
    }
    console.log('futureMonthDate')
    console.log(futureMonthDate)
    this.range[0] = nowdate.year + '-' + nowdate.month + '-' + nowdate.date
    this.range[1] = futureMonthdate.year + '-' + futureMonthdate.month + '-' + futureMonthdate.date
    this.getSecurityNotices()
  }
}
</script>

<style scoped>

</style>
